package org.example.completed.controller;

import org.example.completed.domain.Active;
import org.example.completed.model.vo.ResultVO;
import org.example.completed.service.ActiveService;
import org.example.completed.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 活动
 */
@RestController
@RequestMapping("/active")
public class ActivityController {
    @Autowired
    private ActiveService activeService;


    /**
     * 获取所有的热门活动
     * @return
     */
    @GetMapping("/getAllActives")
    public ResultVO<List<Active>> getAllActives() {
        List<Active> actives = activeService.list();
        return ResultVO.ok(actives, "获取成功");
    }

    /**
     * 按id查询返回对应activity实体类接口
     * @param id
     * @return
     */
    @GetMapping("/getActiveById/{id}")
    public ResultVO<Active> getActiveById(@PathVariable String id) {
        Active active = activeService.getById(id);
        if (active != null) {
            return ResultVO.ok(active, "查询成功");
        }
        return ResultVO.fail("未找到对应的活动");
    }

    /**
     * 返回数据库中id排序的前两个结果以list返回
     * @return
     */
    @GetMapping("/getTopTwoActives")
    public ResultVO<List<Active>> getTopTwoActives() {
        List<Active> actives = activeService.query().orderByAsc("id").last("LIMIT 2").list();
        return ResultVO.ok(actives, "获取成功");
    }

}